﻿
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using OF.Session;
namespace OF.DAL
{
	/// <summary>
	/// 数据访问类:BuyerAddress
	/// </summary>
    public partial class BuyerAddress : Session.DALBase
	{
		public BuyerAddress()
		{}
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "BuyerAddress");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from BuyerAddress");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(OF.Model.BuyerAddress model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into BuyerAddress(");
            strSql.Append("province,city,area,school,address,buyerid,state)");
            strSql.Append(" values (");
            strSql.Append("@province,@city,@area,@school,@address,@buyerid,@state)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@province", SqlDbType.Int,4),
					new SqlParameter("@city", SqlDbType.Int,4),
					new SqlParameter("@area", SqlDbType.Int,4),
					new SqlParameter("@school", SqlDbType.Int,4),
					new SqlParameter("@address", SqlDbType.NVarChar,100),
					new SqlParameter("@buyerid", SqlDbType.Int,4),
					new SqlParameter("@state", SqlDbType.TinyInt,1)};
            parameters[0].Value = model.province;
            parameters[1].Value = model.city;
            parameters[2].Value = model.area;
            parameters[3].Value = model.school;
            parameters[4].Value = model.address;
            parameters[5].Value = model.buyerid;
            parameters[6].Value = model.state;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(OF.Model.BuyerAddress model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update BuyerAddress set ");
            strSql.Append("province=@province,");
            strSql.Append("city=@city,");
            strSql.Append("area=@area,");
            strSql.Append("school=@school,");
            strSql.Append("address=@address,");
            strSql.Append("buyerid=@buyerid,");
            strSql.Append("state=@state");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@province", SqlDbType.Int,4),
					new SqlParameter("@city", SqlDbType.Int,4),
					new SqlParameter("@area", SqlDbType.Int,4),
					new SqlParameter("@school", SqlDbType.Int,4),
					new SqlParameter("@address", SqlDbType.NVarChar,100),
					new SqlParameter("@buyerid", SqlDbType.Int,4),
					new SqlParameter("@state", SqlDbType.TinyInt,1),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.province;
            parameters[1].Value = model.city;
            parameters[2].Value = model.area;
            parameters[3].Value = model.school;
            parameters[4].Value = model.address;
            parameters[5].Value = model.buyerid;
            parameters[6].Value = model.state;
            parameters[7].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from BuyerAddress ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from BuyerAddress ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OF.Model.BuyerAddress GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,province,city,area,school,address,buyerid,state from BuyerAddress ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            OF.Model.BuyerAddress model = new OF.Model.BuyerAddress();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OF.Model.BuyerAddress DataRowToModel(DataRow row)
        {
            OF.Model.BuyerAddress model = new OF.Model.BuyerAddress();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["province"] != null && row["province"].ToString() != "")
                {
                    model.province = int.Parse(row["province"].ToString());
                }
                if (row["city"] != null && row["city"].ToString() != "")
                {
                    model.city = int.Parse(row["city"].ToString());
                }
                if (row["area"] != null && row["area"].ToString() != "")
                {
                    model.area = int.Parse(row["area"].ToString());
                }
                if (row["school"] != null && row["school"].ToString() != "")
                {
                    model.school = int.Parse(row["school"].ToString());
                }
                if (row["address"] != null)
                {
                    model.address = row["address"].ToString();
                }
                if (row["buyerid"] != null && row["buyerid"].ToString() != "")
                {
                    model.buyerid = int.Parse(row["buyerid"].ToString());
                }
                if (row["state"] != null && row["state"].ToString() != "")
                {
                    model.state = int.Parse(row["state"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,province,city,area,school,address,buyerid,state ");
            strSql.Append(" FROM BuyerAddress ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,province,city,area,school,address,buyerid,state ");
            strSql.Append(" FROM BuyerAddress ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM BuyerAddress ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from BuyerAddress T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "BuyerAddress";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return session.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
		#region  ExtensionMethod

        public int Add(string address, string buyerid,string state)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into BuyerAddress(address,buyerid,state) ");
            strSql.Append(" values('" + address + "'," + buyerid + ","+state+")");
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        public int Update(string address, string state,string addressid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" update BuyerAddress set ");
            strSql.Append(" address='" + address + "',state=" + state);
            strSql.Append(" where id="+addressid);
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<OF.Model.BuyerAddress> GetModelList(string strWhere)
        {
            DataSet ds = GetList(strWhere); if (ds != null && ds.Tables[0].Rows.Count > 0) { if (ds != null && ds.Tables[0].Rows.Count > 0) { return DataTableToList(ds.Tables[0]); } else { return null; } } else { return null; }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<OF.Model.BuyerAddress> DataTableToList(DataTable dt)
        {
            List<OF.Model.BuyerAddress> modelList = new List<OF.Model.BuyerAddress>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                OF.Model.BuyerAddress model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetJsonList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,address,buyerid,state ");
            strSql.Append(" FROM BuyerAddress ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by state desc");
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 更新默认状态
        /// </summary>
        public bool UpdateState(int id,int oldstate,int newstate)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" update BuyerAddress");
            strSql.Append(" set state=" + newstate);
            strSql.Append(" where id!=" + id + " and state=" + oldstate);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool DeleteAddress(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" update BuyerAddress");
            strSql.Append(" set state=0");
            strSql.Append(" where id="+id);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		#endregion  ExtensionMethod
	}
}

